package org.csu.forum.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.forum.domain.PO.*;
import org.csu.forum.domain.VO.*;
import org.csu.forum.mapper.*;
import org.csu.forum.service.AccountService;
import org.csu.forum.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private TitlepostMapper titlepostMapper;

    @Autowired
    private ReplyContentMapper replyContentMapper;

    @Autowired
    private CollectpostMapper collectpostMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private UserpriorMapper userpriorMapper;

    @Autowired
    private BasicPriorMapper basicpriorMapper;

    @Autowired
    private ManagePriorMapper managePriorMapper;

    @Autowired
    private UserSendPostMapper userSendPostMapper;

    @Autowired
    private UserTitlePostMapper userTitlePostMapper;

    @Autowired
    private FollowpostMapper followpostMapper;

    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private ZoneMapper zoneMapper;

    public Account getAccount(int userid) {

        //对userinfo表进行查询
        Account account = new Account();
        QueryWrapper<Account>accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("userid",userid);
        account = accountMapper.selectOne(accountQueryWrapper);

        //表中没有该用户，返回空值
        if (account == null)
        {
            return null;
        }

        //返回获取到的用户结果
        return account;

    }


    public Boolean isUseridExist(int userid) {

        //通过获取账户接口判断有无用户信息
        if(getAccount(userid) == null){
            return false;
        }
        return true;

    }


    public String getToken(int userid, String password) {

        //查询login表
        QueryWrapper<Login>loginQueryWrapper = new QueryWrapper<>();
        loginQueryWrapper.eq("userid",userid).eq("password",password);

        //查看用户ID和密码是否存在且正确
        if(loginMapper.selectOne(loginQueryWrapper) != null){

            //登录成功返回一个token信息
            String token = TokenUtils.token(userid);
            return token;

        }
        return null;
    }


    public void insertAccount(AccountregisterVO accountregisterVO) {

        //插入用户信息表
        accountMapper.insert(accountregisterVO.getAccount());
        //插入用户登录表
        loginMapper.insert(accountregisterVO.getLogin());

    }


    public void updateAccount(Account account) {

        //根据用户ID来修改用户信息
        UpdateWrapper<Account>accountUpdateWrapper = new UpdateWrapper<>();
        accountUpdateWrapper.eq("userid", account.getUserid());
        accountMapper.update(account,accountUpdateWrapper);

    }


    public List<TitlepostContentVO> getTitlepostContentVOList(int userid) {

        List<TitlepostContentVO> titlepostContentVOList = new ArrayList<>();
        List<UserTitlePost> userTitlePostList = new ArrayList<>();

        QueryWrapper<UserTitlePost>userTitlePostQueryWrapper = new QueryWrapper<>();
        userTitlePostQueryWrapper.eq("userid",userid);
        userTitlePostList = userTitlePostMapper.selectList(userTitlePostQueryWrapper);

        Iterator<UserTitlePost> iterator = userTitlePostList.listIterator();
        while(iterator.hasNext())
        {
            UserTitlePost userTitlePost = iterator.next();
            TitlepostContentVO titlepostContentVO = new TitlepostContentVO();
            titlepostContentVO.setUserTitlePost(userTitlePost);

            titlepostContentVOList.add(titlepostContentVO);
        }

        //对插入部分值的VOList进行迭代查询
        Iterator<TitlepostContentVO>iterator1 = titlepostContentVOList.listIterator();
        while(iterator1.hasNext())
        {

            TitlepostContentVO titlepostContentVO = iterator1.next();

            Titlepost titlepost = new Titlepost();
            QueryWrapper<Titlepost>titlepostQueryWrapper = new QueryWrapper<>();
            titlepostQueryWrapper.eq("post_id",titlepostContentVO.getUserTitlePost().getPostid());
            titlepost = titlepostMapper.selectOne(titlepostQueryWrapper);

            titlepostContentVO.setTitlepost(titlepost);


            Zone zone = new Zone();
            QueryWrapper<Zone>zoneQueryWrapper = new QueryWrapper<>();
            zoneQueryWrapper.eq("zoneid",titlepostContentVO.getTitlepost().getZoneid());
            zone = zoneMapper.selectOne(zoneQueryWrapper);

            titlepostContentVO.setZone(zone);

            titlepostContentVO.setParameter();

        }


        return titlepostContentVOList;


    }


    public void insertTitlepost(TitlepostEditVO titlepostEditVO) {

        Titlepost titlepost = new Titlepost();

        //将浏览器获取的信息放入titlepost对象中，并将没有的给出的值设置为默认值false
        titlepost.setContent(titlepostEditVO.getContent());
        titlepost.setIsvote(titlepostEditVO.getIsvote());
        titlepost.setZoneid(titlepostEditVO.getZoneid());
        titlepost.setIsesscence(false);
        titlepost.setIsreport(false);
        titlepost.setIstop(false);
        titlepostMapper.insert(titlepost);


    }


    public Titlepost updateTitlepost(Titlepost titlepost)   {

        Titlepost titlepost1 = new Titlepost();
        //利用新建的titlepost对象进行表的更新
        UpdateWrapper<Titlepost>titlepostUpdateWrapper = new UpdateWrapper<>();
        titlepostUpdateWrapper.eq("post_id",titlepost.getPostid());
        titlepost1 = titlepostMapper.selectOne(titlepostUpdateWrapper);

        titlepost1.setContent(titlepost.getContent());
        titlepost1.setZoneid(titlepost.getZoneid());
        if(titlepost.getIsvote() == null)
        {
            titlepost1.setIsvote(false);
        }


        titlepostMapper.update(titlepost1,titlepostUpdateWrapper);

        return titlepost1;
    }


    public TitlepostContentVO deleteTitlepost(int userid,int postid) {

        TitlepostContentVO titlepostContentVO = new TitlepostContentVO();

        //查找usertitilepost表并进行删除
        UserTitlePost userTitlePost = new UserTitlePost();
        userTitlePost.setUserid(userid);
        userTitlePost.setPostid(postid);
        titlepostContentVO.setUserTitlePost(userTitlePost);
        QueryWrapper<UserTitlePost>userTitlePostQueryWrapper = new QueryWrapper<>();
        userTitlePostQueryWrapper.eq("userid",userid).eq("post_id",postid);
        userTitlePostMapper.delete(userTitlePostQueryWrapper);

        //查找titlepost表并进行删除
        Titlepost titlepost = new Titlepost();
        QueryWrapper<Titlepost> titlepostQueryWrapper = new QueryWrapper<>();
        titlepostQueryWrapper.eq("post_id",postid);
        titlepost = titlepostMapper.selectOne(titlepostQueryWrapper);
        titlepostMapper.delete(titlepostQueryWrapper);
        titlepostContentVO.setTitlepost(titlepost);

        //查找zone表获取值
        Zone zone = new Zone();
        QueryWrapper<Zone> zoneQueryWrapper = new QueryWrapper<>();
        zoneQueryWrapper.eq("zoneid",titlepost.getZoneid());
        zone = zoneMapper.selectOne(zoneQueryWrapper);
        titlepostContentVO.setZone(zone);

        titlepostContentVO.setParameter();

        return titlepostContentVO;
    }



    public List<ReplyCheckVO> getReplyCheckVOList(int userid) {

        List<ReplyCheckVO> replyCheckVOList = new ArrayList<>();
        List<ReplyContent> replyContentList = new ArrayList<>();
        List<Post> postList = new ArrayList<>();

        //通过用户ID在collectpost表中查找
        QueryWrapper<ReplyContent>replyContentQueryWrapper = new QueryWrapper<>();
        replyContentQueryWrapper.eq("replyer_id",userid);
        replyContentList = replyContentMapper.selectList(replyContentQueryWrapper);

        //用迭代器进行迭代
        Iterator<ReplyContent>iterator =replyContentList.listIterator();
        while(iterator.hasNext())
        {
            //将获取的ReplyContent放入VO对象中
            ReplyContent replyContent =iterator.next();
            ReplyCheckVO replyCheckVO = new ReplyCheckVO();
            replyCheckVO.setReplyContent(replyContent);

            //将VO对象插入List中
            replyCheckVOList.add(replyCheckVO);
        }

        //对插入部分值的VOList进行迭代查询
        Iterator<ReplyCheckVO>iterator1 = replyCheckVOList.listIterator();
        while(iterator1.hasNext())
        {
            //通过ReplyContent中的id查找post表中对应id的内容
            Post post = new Post();
            ReplyCheckVO replyCheckVO = iterator1.next();
            QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
            postQueryWrapper.eq("post_id",replyCheckVO.getReplyContent().getPostid());
            post = postMapper.selectOne(postQueryWrapper);

            //将查询到的post插入到VO中
            replyCheckVO.setPost(post);

            replyCheckVO.setParameter();

            System.out.println("11354543");
        }



        return replyCheckVOList;

    }

    @Override
    public ReplyCheckVO updateReplyCheckVO(ReplyContent replyContent) {

        ReplyContent replyContent1 = new ReplyContent();
        //利用新建的replycontent1对象进行表的更新
        UpdateWrapper<ReplyContent>replyContentUpdateWrapper = new UpdateWrapper<>();
        replyContentUpdateWrapper.eq("reply_id",replyContent.getReplyid());
        replyContent1 = replyContentMapper.selectOne(replyContentUpdateWrapper);

        replyContent1.setContent(replyContent.getContent());

        replyContentMapper.update(replyContent1,replyContentUpdateWrapper);

        ReplyCheckVO replyCheckVO = new ReplyCheckVO();
        Post post = new Post();
        replyCheckVO.setReplyContent(replyContent1);
        QueryWrapper<Post> postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.eq("post_id",replyContent1.getPostid());
        post = postMapper.selectOne(postQueryWrapper);
        replyCheckVO.setPost(post);

        replyCheckVO.setParameter();

        System.out.println("516");
        return replyCheckVO;
    }


    public ReplyCheckVO deleteReplyCheckVO(int userid,int replyid){

        ReplyCheckVO replyCheckVO = new ReplyCheckVO();
        ReplyContent replyContent = new ReplyContent();
        Post post = new Post();

        QueryWrapper<ReplyContent>replyContentQueryWrapper = new QueryWrapper<>();
        replyContentQueryWrapper.eq("reply_id",replyid);
        replyContent = replyContentMapper.selectOne(replyContentQueryWrapper);
        replyCheckVO.setReplyContent(replyContent);
        replyContentMapper.delete(replyContentQueryWrapper);

        QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.eq("post_id",replyContent.getPostid());
        post = postMapper.selectOne(postQueryWrapper);
        replyCheckVO.setPost(post);

        replyCheckVO.setParameter();

        return replyCheckVO;

    }


    public List<CollectpostContentVO> getCollectpostContentVOList(int userid){

        List<CollectpostContentVO> collectpostContentVOList = new ArrayList<>();
        List<Collectpost> collectpostList = new ArrayList<>();
        List<Post> postList = new ArrayList<>();

        //通过用户ID在collectpost表中查找
        QueryWrapper<Collectpost>collectpostQueryWrapper = new QueryWrapper<>();
        collectpostQueryWrapper.eq("userid",userid);
        collectpostList = collectpostMapper.selectList(collectpostQueryWrapper);

        //用迭代器进行迭代
        Iterator<Collectpost>iterator =collectpostList.listIterator();
        while(iterator.hasNext())
        {
            //将获取的Collectpost放入VO对象中
            Collectpost collectpost =iterator.next();
            CollectpostContentVO collectpostContentVO = new CollectpostContentVO();
            collectpostContentVO.setCollectpost(collectpost);

            //将VO对象插入List中
            collectpostContentVOList.add(collectpostContentVO);
        }

        //对插入部分值的VOList进行迭代查询
        Iterator<CollectpostContentVO>iterator1 = collectpostContentVOList.listIterator();
        while(iterator1.hasNext())
        {
            //通过collectpost中的id查找post表中对应id的内容
            Post post = new Post();
            CollectpostContentVO collectpostContentVO = iterator1.next();
            QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
            postQueryWrapper.eq("post_id",collectpostContentVO.getCollectpost().getCollectPostId());
            post = postMapper.selectOne(postQueryWrapper);

            //将查询到的post插入到VO中
            collectpostContentVO.setPost(post);

            collectpostContentVO.setParameter();
        }


        return collectpostContentVOList;
    }


    public CollectpostContentVO deleteCollectpost(int userid,int collectpostid){

        CollectpostContentVO collectpostContentVO= new CollectpostContentVO();
        Collectpost collectpost = new Collectpost();
        Post post = new Post();

        QueryWrapper<Collectpost>collectpostQueryWrapper = new QueryWrapper<>();
        collectpostQueryWrapper.eq("collect_post_id",collectpostid).eq("userid",userid);
        collectpost = collectpostMapper.selectOne(collectpostQueryWrapper);
        collectpostContentVO.setCollectpost(collectpost);
        collectpostMapper.delete(collectpostQueryWrapper);

        QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.eq("post_id",collectpost.getCollectPostId());
        post = postMapper.selectOne(postQueryWrapper);
        collectpostContentVO.setPost(post);

        collectpostContentVO.setParameter();

        return collectpostContentVO;
    }


    public List<AllPostContentVO> getAllPostContentVOList(int userid) {



        List<AllPostContentVO> allPostContentVOList = new ArrayList<>();
        List<UserSendPost> userSendPostList = new ArrayList<>();


        //获取userSendPost对象并插入VO对象中
        QueryWrapper<UserSendPost> userSendPostQueryWrapper = new QueryWrapper<>();
        userSendPostQueryWrapper.eq("userid",userid);
        userSendPostList = userSendPostMapper.selectList(userSendPostQueryWrapper);

        Iterator<UserSendPost> iterator = userSendPostList.listIterator();
        while(iterator.hasNext())
        {
            UserSendPost userSendPost = iterator.next();
            AllPostContentVO allPostContentVO = new AllPostContentVO();
            allPostContentVO.setUserSendPost(userSendPost);

            allPostContentVOList.add(allPostContentVO);

        }

        //迭代器插入各个对象
        Iterator<AllPostContentVO> iterator1 = allPostContentVOList.listIterator();
        while(iterator1.hasNext())
        {

            AllPostContentVO allPostContentVO = iterator1.next();
            Post post = new Post();
            QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
            postQueryWrapper.eq("post_id",allPostContentVO.getUserSendPost().getPostid());
            post = postMapper.selectOne(postQueryWrapper);

            allPostContentVO.setPost(post);


            Files files = new Files();
            QueryWrapper<Files>fileQueryWrapper = new QueryWrapper<>();
            fileQueryWrapper.eq("file_id",allPostContentVO.getPost().getFileid());
            files = fileMapper.selectOne(fileQueryWrapper);

            allPostContentVO.setFiles(files);

            Followpost followpost = new Followpost();
            QueryWrapper<Followpost>followpostQueryWrapper = new QueryWrapper<>();
            followpostQueryWrapper.eq("followpost_id",allPostContentVO.getPost().getPostid());
            followpost = followpostMapper.selectOne(followpostQueryWrapper);

            allPostContentVO.setFollowpost(followpost);


            Titlepost titlepost = new Titlepost();
            QueryWrapper<Titlepost>titlepostQueryWrapper = new QueryWrapper<>();
            titlepostQueryWrapper.eq("post_id",allPostContentVO.getFollowpost().getTitlepostId());
            titlepost = titlepostMapper.selectOne(titlepostQueryWrapper);

            allPostContentVO.setTitlepost(titlepost);

            allPostContentVO.setParameter();

        }


        return allPostContentVOList;
    }



    public PostEditVO deletePostContent(int postid) {

        PostEditVO postEditVO = new PostEditVO();
        Post post = new Post();
        Files files = new Files();
        Followpost followpost = new Followpost();

        QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.eq("post_id",postid);
        post = postMapper.selectOne(postQueryWrapper);
        postEditVO.setPost(post);

        QueryWrapper<Files>fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq("file_id",post.getFileid());
        files = fileMapper.selectOne(fileQueryWrapper);
        postEditVO.setFiles(files);

        QueryWrapper<Followpost>followpostQueryWrapper = new QueryWrapper<>();
        followpostQueryWrapper.eq("followpost_id",postid);
        followpost = followpostMapper.selectOne(followpostQueryWrapper);
        postEditVO.setFollowpost(followpost);
        fileMapper.delete(fileQueryWrapper);

        postEditVO.setParameter();

        return postEditVO;
    }


    public PostEditVO updatePostContent(PostEditVO postEditVO) {

        Post post = new Post();
        Followpost followpost = new Followpost();
        Files files = new Files();

        QueryWrapper<Post>postQueryWrapper = new QueryWrapper<>();
        postQueryWrapper.eq("post_id",postEditVO.getPost().getPostid());
        post = postMapper.selectOne(postQueryWrapper);
        post.setContent(postEditVO.getPost().getContent());
        post.setZoneid(postEditVO.getPost().getZoneid());
        post.setFileid(postEditVO.getPost().getFileid());
        postEditVO.setPost(post);
        postMapper.update(post,postQueryWrapper);


        QueryWrapper<Followpost>followpostQueryWrapper = new QueryWrapper<>();
        followpostQueryWrapper.eq("followpost_id",postEditVO.getPost().getPostid());
        followpost = followpostMapper.selectOne(followpostQueryWrapper);
        followpost.setTitlepostId(postEditVO.getFollowpost().getTitlepostId());
        postEditVO.setFollowpost(followpost);
        followpostMapper.update(followpost,followpostQueryWrapper);

        QueryWrapper<Files>fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq("file_id",postEditVO.getPost().getFileid());
        files = fileMapper.selectOne(fileQueryWrapper);
        postEditVO.setFiles(files);

        postEditVO.setParameter();
        return postEditVO;
    }


    public List<AllPriorVO> getAllPriorVOList(int userid) {

        List<AllPriorVO> allPriorVOList = new ArrayList<>();
        List<Userprior> userpriorList = new ArrayList<>();
        List<Basicprior> basicpriorList = new ArrayList<>();
        List<ManagePrior> managePriorList = new ArrayList<>();

        //查找出所有userprior对象并将其放入allPriorVO列表中
        QueryWrapper<Userprior>userpriorQueryWrapper = new QueryWrapper<>();
        userpriorQueryWrapper.eq("userid",userid);
        userpriorList = userpriorMapper.selectList(userpriorQueryWrapper);

        Iterator<Userprior> iterator = userpriorList.listIterator();
        while(iterator.hasNext())
        {
            Userprior userprior = iterator.next();
            AllPriorVO allPriorVO = new AllPriorVO();
            allPriorVO.setUserprior(userprior);

            allPriorVOList.add(allPriorVO);
        }

        //用迭代器对AllPriorVO列表进行查询，将对应的剩余对象插入其中
        Iterator<AllPriorVO> iterator1 = allPriorVOList.listIterator();
        while(iterator1.hasNext())
        {

            //插入basicprior对象
            AllPriorVO allPriorVO = iterator1.next();
            Basicprior basicprior = new Basicprior();
            QueryWrapper<Basicprior>basicpriorQueryWrapper = new QueryWrapper<>();
            basicpriorQueryWrapper.eq("basic_pid",allPriorVO.getUserprior().getBasicpid());
            basicprior = basicpriorMapper.selectOne(basicpriorQueryWrapper);

            allPriorVO.setBasicprior(basicprior);


            //插入ManagePrior对象
            ManagePrior managePrior = new ManagePrior();
            QueryWrapper<ManagePrior>managePriorQueryWrapper = new QueryWrapper<>();
            managePriorQueryWrapper.eq("manage_pid",allPriorVO.getUserprior().getManagepid());
            managePrior = managePriorMapper.selectOne(managePriorQueryWrapper);

            allPriorVO.setManagePrior(managePrior);
        }

        return allPriorVOList;



    }


}
